import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-11-26
 * Time: 21:39
 */
//五：消失的两个数字
class Solution10 {
    public int[] missingTwo(int[] nums) {
        //创建tem数组用来存放nums数组中的元素和1~N数字
        int len = nums.length;
        int n = len + 2;
        int[] tem = new int[2*len+2];
        //处理nums数组
        int ret = 0;
        for(int i = 0 ; i < nums.length ; i++){
            ret ^= nums[i];
            tem[i] = nums[i];
        }
        //处理1~N
        for(int i = 1 ; i <= n ; i++){
            ret ^= i;
            tem[len-1+i] = i;
        }
        int sign = 0;
        while(ret != 0){
            if( ((ret >> sign) & 1) == 1){
                break;
            }else{
                sign++;
            }
        }

        //基于sign把数组分成两组
        int[] result = new int[2];
        for(int x : tem){
            if(((x >> sign) & 1) == 1){
                result[0] ^= x;
            }else{
                result[1] ^= x;
            }
        }

        return result;



    }
}
class Solution10_1 {
    //穷举法
    public int[] missingTwo(int[] nums) {
        Arrays.sort(nums);
        int len = nums.length;
        int[] ret = new int[2];
        int j = 0;
        for(int i = 0 ; i < nums.length-1 ; i++){
            int tem = nums[i+1] - nums[i];
            if(tem == 2){
                ret[j++] = nums[i]+1;
            }else if(tem == 3){
                ret[j++] = nums[i]+1;
                ret[j] = nums[i]+2;
            }
        }

        if(nums[0] != 1){
            if(ret[0] == 0){
                ret[0] = 1;
                if(len == 1){
                    if(nums[0]-1 > 1){
                        ret[1] = 2;
                    }else{
                        ret[1] = 3;
                    }
                }else{
                    ret[1] = nums[len-1]+1;
                }

            }else{
                if(ret[1] == 0){
                    ret[1] = 1;
                }else{

                }
            }
        }else{
            if(ret[0] == 0){
                ret[0] = nums[len-1]+1;
                ret[1] = nums[len-1]+2;
            }else{
                if(ret[1] == 0){
                    ret[1] = nums[len-1]+1;
                }else{

                }
            }
        }
        return ret;

    }
}
public class Test10 {
}
